package tdh.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tdh.common.util.Util;
import tdh.dao.DepartDao;
import tdh.dao.UserDao;
import tdh.entity.po.User;
import tdh.entity.vo.UserVo;
import tdh.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户信息业务接口实现类
 * @author 曹昊
 * @date 2021/7/12
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    DepartDao departDao;
    @Autowired
    Util util;



    /**
     * @Author caohao
     * @Description 登陆时的账户密码检测
     * @Date 10:18 2021/7/12
     * @Param [username, password]
     * @return boolean
     **/
    @Override
    public boolean loginCheck(String username, String password) {
        User user = new User();
        user.setYhid(username);
        user.setYhkl(password);
        List<User> select = userDao.select(user);
        if (select.size()==0 || select==null) {
            return false;
        }
        return true;
    }

    /**
     * @Author caohao
     * @Description 查询全部用户
     * @Date 10:18 2021/7/12
     * @Param []
     * @return java.util.List<tdh.entity.vo.UserVo>
     **/
    @Override
    public List<UserVo> allUsers() {
        ArrayList<UserVo> userVos = new ArrayList<UserVo>();
        for (User user : userDao.select(new User())) {
            String yhbm = util.getBmmcByBmdm(user.getYhbm());
            String yhxb = util.getYhxbByXbCode(user.getYhxb());
            userVos.add(UserVo.tsfVo(user,yhxb,yhbm));
        }
        return userVos;
    }

    @Override
    public List<UserVo> allUsersByYhid(String yhid) {
        ArrayList<UserVo> userVos = new ArrayList<UserVo>();
        List<User> userList = userDao.yhidUsers(yhid);
        for (User tmp :  userList) {
            String yhbm = util.getBmmcByBmdm(tmp.getYhbm());
            String yhxb = util.getYhxbByXbCode(tmp.getYhxb());
            userVos.add(UserVo.tsfVo(tmp,yhxb,yhbm));
        }
        return userVos;
    }

    /**
     * @Author caohao
     * @Description 根据部门名称查询用户列表
     * @Date 10:19 2021/7/12
     * @Param [bmdm]
     * @return java.util.List<tdh.entity.vo.UserVo>
     **/
    @Override
    public List<UserVo> allUsersByBmmc(String bmdm) {
        ArrayList<UserVo> userVos = new ArrayList<UserVo>();
        User user = new User();
        user.setYhbm(bmdm);
        List<User> userList = userDao.select(user);
        for (User tmp :  userList) {
            String yhbm = util.getBmmcByBmdm(tmp.getYhbm());
            String yhxb = util.getYhxbByXbCode(tmp.getYhxb());
            userVos.add(UserVo.tsfVo(tmp,yhxb,yhbm));
        }
        return userVos;
    }

    /**
     * @Author caohao
     * @Description 根据用户姓名模糊查询用户
     * @Date 15:39 2021/7/12
     * @Param [yhxm]
     * @return java.util.List<tdh.entity.vo.UserVo>
     **/
    @Override
    public List<UserVo> yhxmUsers(String yhxm) {
        ArrayList<UserVo> userVos = new ArrayList<UserVo>();
        List<User> userList = userDao.yhxmUsers(yhxm);
        for (User tmp :  userList) {
            String yhbm = util.getBmmcByBmdm(tmp.getYhbm());
            String yhxb = util.getYhxbByXbCode(tmp.getYhxb());
            userVos.add(UserVo.tsfVo(tmp,yhxb,yhbm));
        }
        return userVos;
    }

    /**
     * @Author caohao
     * @Description 根据用户代码查询用户
     * @Date 10:19 2021/7/12
     * @Param [yhdm]
     * @return tdh.entity.vo.UserVo
     **/
    @Override
    public UserVo userByYhdm(String yhdm) throws RuntimeException{
        User user = new User();
        user.setYhdm(yhdm);
        List<User> userList = userDao.select(user);
        if (userList.size()==0) {
            throw new RuntimeException("数据库查询失败，查询结果为空");
        }
        user = userList.get(0);
        String yhbm = util.getBmmcByBmdm(user.getYhbm());
        String yhxb = util.getYhxbByXbCode(user.getYhxb());
        return UserVo.tsfVo(user,yhxb,yhbm);
    }

    /**
     * @Author caohao
     * @Description 新增用户
     * @Date 10:19 2021/7/12
     * @Param [user]
     * @return void
     **/
    @Override
    public void insertOne(User user) {
        String nowTime = Util.nowTime();
        user.setDjsj(nowTime);
        userDao.insertOne(user);
    }

    /**
     * @Author caohao
     * @Description 删除用户
     * @Date 10:19 2021/7/12
     * @Param [yhdm]
     * @return void
     **/
    @Override
    public void deleteOne(String yhdm) {
        userDao.deleteOne(yhdm);
    }

    /**
     * @Author caohao
     * @Description 更新用户信息
     * @Date 10:19 2021/7/12
     * @Param [user]
     * @return void
     **/
    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }
}
